package com.lili.setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lili.model.setting.Role;
import com.lili.model.setting.RoleMenu;
import com.lili.setting.mapper.RoleMapper;
import com.lili.setting.mapper.RoleMenuMapper;
import com.lili.setting.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
@SuppressWarnings("all") // 抑制警告
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public void saveRole(Role role) {
        roleMapper.insert(role);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setRoleAuth(String roleId, List<RoleMenu> list) {
        // 先情况角色的权限
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        roleMenuMapper.delete(wrapper);

        // 遍历添加角色权限
        list.stream().forEach(roleMenu -> {
            roleMenuMapper.insert(roleMenu);
        });
    }


}
